package com.framework.modules.sys.controller;

import com.framework.exception.util.RespResult;
import com.framework.modules.oss.cloud.OSSFactory;
import com.framework.page.PageUtils;
import com.framework.service.SysNoticeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;


/**
 * 系统日志
 */
@RestController
@RequestMapping("/sys/notice")
public class SysNoticeController extends AbstractController {
	@Autowired
	private SysNoticeService sysNoticeService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:notice:list")
	public RespResult list(@RequestParam Map<String, Object> params) {
		if(params.get("deptId")==null ||"".equals(params.get("deptId")) ){
			params.put("deptId",getUser().getDeptId());
		}
		PageUtils page = sysNoticeService.queryNoticeList(params);
		return RespResult.ok().put("page", page);
	}

	/**
	 * 增加通知
	 * @param file
	 * @param request
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/add")
	public RespResult addNotice(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
		Long deptId=getUser().getDeptId();

		String prefix="";
		if(deptId.equals(1L)){
			prefix="banner";
		}else if(deptId.equals(889323L)){
			prefix="kayi";
		}else if(deptId.equals(889324L)){
			prefix="xinchuang";
		}else if(deptId.equals(889325L)){
			prefix="suchuangbang";
		}
		String coverSuffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
		String coverUrl = OSSFactory.build().uploadSuffix(file.getBytes(), coverSuffix,prefix);
		sysNoticeService.insertNewNotice(coverUrl,deptId,request);
		return RespResult.ok();
	}

	@RequestMapping("/delete/{sysNoticeNo}")
	public RespResult delete(@PathVariable String sysNoticeNo){
		sysNoticeService.deleteById(sysNoticeNo);
		return  RespResult.ok();
	}

}
